package org.ourpioneer.excel.excute.process;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.ourpioneer.excel.excute.entity.FromEntity;

public class ReadFile {
	
	int countAll = 0;
	
	/**
	 * 读取Excel2003
	 * 
	 * @param filePath
	 * @return
	 */
	public List<FromEntity> readFromXLS2003(String filePath) {
		File excelFile = null;// Excel文件对象
		InputStream is = null;// 输入流对象
		String cellStr = null;// 单元格，最终按字符串处理
		List<FromEntity> fromEntityList = new ArrayList<FromEntity>();// 返回封装数据的List
		FromEntity fromEntity = null;// 每一个初始对象
		try {
			excelFile = new File(filePath);
			is = new FileInputStream(excelFile);// 获取文件输入流
			HSSFWorkbook workbook2003 = new HSSFWorkbook(is);// 创建Excel2003文件对象
			HSSFSheet sheet = workbook2003.getSheetAt(0);// 取出第一个工作表，索引是0
			// 开始循环遍历行，表头不处理，从2开始
			for (int i = 2; i <= sheet.getLastRowNum(); i++) {
				countAll++;
				fromEntity = new FromEntity();// 实例化Student对象
				HSSFRow row = sheet.getRow(i);// 获取行对象
				if (row == null) {// 如果为空，不处理
					continue;
				}
				// 循环遍历单元格
				for (int j = 0; j < row.getLastCellNum(); j++) {
					HSSFCell cell = row.getCell(j);// 获取单元格对象
					if (cell == null) {// 单元格为空设置cellStr为空串
						cellStr = "";
					} else {// 其余按照字符串处理
						cellStr = cell.getStringCellValue();
					}
					
					// 下面按照数据出现位置封装到bean中
					if (j == 0) {
						fromEntity.setFilePath(cellStr);
					} else if (j == 1) {
						fromEntity.setFileName(cellStr);
					} else if (j == 2) {
						fromEntity.setChineseName(cellStr);
					} else if (j == 3) {
						fromEntity.setEnglishName(cellStr);
					} else {
						System.out.println("ERROR:第"+ i +"行原始属性多于4个，需查看！！！！！");
					}
				}
				fromEntityList.add(fromEntity);// 数据装入List
				workbook2003.close();
				System.out.println(countAll);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {// 关闭文件流
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return fromEntityList;
	}


}
